// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// h [author] rhyme_lph
// f [email]  rhymelph@gmail.com
// c [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/employee_contact_entity.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/hr_payroll/leave/select_leave_type_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "selectLeaveTypePage")
class SelectLeaveTypePage extends StatefulWidget {
  final EmployeeContactEntity data;
  final EmployeeContactEmployeeLeaves select;

  const SelectLeaveTypePage({Key key, this.data, this.select})
      : super(key: key);

  @override
  _SelectLeaveTypePageState createState() => _SelectLeaveTypePageState();
}

class _SelectLeaveTypePageState extends State<SelectLeaveTypePage>
    with PagePresenter<SelectLeaveTypePage, SelectLeaveTypeProvider> {
  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.leave_type);

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            Padding(
              padding: EdgeInsets.only(
                  right: getDp(39), top: getDp(21), bottom: getDp(21)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    S.current.balance,
                    style: TextStyle(
                      color: aceBlackColor,
                      fontSize: getSp(18),
                      fontWeight: FontWeight.w700,
                      height: 16 / 18,
                    ),
                  ),
                ],
              ),
            ),
            ...widget.data.employeeLeaves.map((e) {
              print('total:${e.total} , used:${e.used}');
              return PressedOpacityButton(
                padding: EdgeInsets.only(
                  bottom: getDp(24),
                ),
                onPressed: () => provider.onSelect(e),
                child: _buildLeaveData(
                    title: e?.name ?? '',
                    amount: (e.total - e.used)?.toString(),
                    isSelect: widget.select?.type == e?.type),
              );
            }).toList(),
          ],
        ),
      );

  Row _buildLeaveData({String title, String amount, bool isSelect}) {
    return Row(
      children: [
        SizedBox(
          width: getDp(29),
        ),
        Expanded(
          child: NormalRadio(
            title: title,
            isSelect: isSelect,
            isFix: true,
          ),
        ),
        Container(
          margin: EdgeInsets.only(
            right: getDp(39),
          ),
          width: getDp(60),
          alignment: Alignment.center,
          child: Text(
            amount,
            style: TextStyle(
              color: aceBlackColor,
              fontSize: getSp(18),
              fontWeight: FontWeight.w400,
              height: 16 / 18,
            ),
          ),
        ),
      ],
    );
  }
}
